package com.thinkgem.jeesite.modules.guobo.webservice;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.modules.guobo.dao.AssetDao;
import com.thinkgem.jeesite.modules.guobo.dao.TrailLogDao;
import com.thinkgem.jeesite.modules.guobo.entity.Asset;
import com.thinkgem.jeesite.modules.guobo.entity.Rw;
import com.thinkgem.jeesite.modules.guobo.entity.TrailLog;
import com.thinkgem.jeesite.modules.guobo.entity.WarningLog;
import com.thinkgem.jeesite.modules.guobo.entity.WebServiceResult;
import com.thinkgem.jeesite.modules.guobo.service.RwService;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.User;

import net.sf.json.JSONObject;

@Component("warningLogWebService")
public class WarningLogWebService {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private RwService rwService;
	
	@Autowired
	private TrailLogDao trailLogDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private AssetDao assetDao;
	/**
	 * 添加报警信息
	 * @return
	 */
	@Transactional(readOnly = false)
	public String addWarningLog(String warningLog){
		logger.info(warningLog);
		try{
			JSONObject jsonObject = JSONObject.fromObject(warningLog);  
			WarningLog log=(WarningLog)JSONObject.toBean(jsonObject, WarningLog.class);			
			String rfid = log.getRfid();
			if(rfid!=null && !"".equals(rfid) && !"null".equals(rfid)){
				String[] rfids = rfid.split("-");
				for (String rf : rfids) {
					TrailLog trailLog = new TrailLog();
					Asset temp = new Asset();
					temp.setRfid(rf);
					Asset as = assetDao.getAssetByRfid(temp);
					trailLog.setAsset(temp);
					Rw rw = rwService.getRwByAddress(log.getReaderAdr());
					trailLog.setRw(rw);
					String inOrOut = log.getInOutStatus();
					String motionType="";
					if(inOrOut!=null && !"null".equals(inOrOut) && !"".equals(inOrOut)){
						//可能为主标签
						if(as!=null && !as.getExhHall().getId().equals(rw.getLocation().getExhHall().getId())){
							as.setExhHall(rw.getLocation().getExhHall());
							assetDao.update(as);
						}
						if(inOrOut.equals("0")){
							motionType="in";
						}else{
							motionType="out";
						}
					}
					trailLog.setMotionType(motionType);
					trailLog.setStatus("0");
					trailLog.setIsWarn(log.getIsWarn());
					trailLog.setGrantEpc(log.getGrantEpc());
					trailLog.setIsDispatched(log.getIsDispatched());
					trailLog.setReaderAdr(log.getReaderAdr());
					trailLog.preInsert();
					User user = new User();
					user.setLoginName("admin");
					user = userDao.getByLoginName(user);
					trailLog.setCreateBy(user);
					trailLog.setUpdateBy(user);
					trailLogDao.insert(trailLog);
					
				}
				WebServiceResult result = new WebServiceResult();
				result.setResult("0");;
				result.setResult("success");
				JSONObject json = JSONObject.fromObject(result);//将java对象转换为json对象  
		        String str = json.toString();//将json对象转换为字符串  
		        logger.info(str);
				return str;
			}
			WebServiceResult result = new WebServiceResult();
			result.setResult("fail");
			result.setMessage("报警信息上传成功");
			JSONObject json = JSONObject.fromObject(result);//将java对象转换为json对象  
			String str = json.toString();//将json对象转换为字符串  
	        System.out.println(str);  
			return str;		
			
		}catch(Exception e){
			e.printStackTrace();
			WebServiceResult result = new WebServiceResult();
			result.setResult("fail");
			result.setMessage("报警信息上传失败");
			JSONObject json = JSONObject.fromObject(result);//将java对象转换为json对象  
			String str = json.toString();//将json对象转换为字符串  
	        System.out.println(str);  
			return str;
		}
		
	}
}
